package com.yaorange.teaching.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.yaorange.common.core.constant.SecurityConstants;
import com.yaorange.common.core.domain.R;
import com.yaorange.common.security.annotation.InnerAuth;
import com.yaorange.teaching.bo.DailyTaskSubmitCountBo;
import com.yaorange.teaching.service.AIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import com.yaorange.common.log.annotation.Log;
import com.yaorange.common.log.enums.BusinessType;
import com.yaorange.common.security.annotation.RequiresPermissions;
import com.yaorange.teaching.domain.YaDailyTasks;
import com.yaorange.teaching.service.IYaDailyTasksService;
import com.yaorange.common.core.web.controller.BaseController;
import com.yaorange.common.core.web.domain.AjaxResult;
import com.yaorange.common.core.utils.poi.ExcelUtil;
import com.yaorange.common.core.web.page.TableDataInfo;
import reactor.core.publisher.Flux;

/**
 * 每日任务Controller
 *
 * @author ruoyi
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/daily_tasks")
public class YaDailyTasksController extends BaseController
{
    @Autowired
    private IYaDailyTasksService yaDailyTasksService;

    @Autowired
    private AIService aiService;

    @RequiresPermissions("teaching:daily_tasks:list")
    @GetMapping("/list/interview")
    public TableDataInfo queryInterviewList(@RequestParam("date")String date,@RequestParam("classesId")Long classesId)
    {
        List<YaDailyTasks> list = yaDailyTasksService.queryInterviewList(date,classesId);
        return getDataTable(list);
    }


    /**
     * 查询每日任务提交汇总
     */
    @RequiresPermissions("teaching:daily_tasks:list")
    @GetMapping("/submit/count")
    public AjaxResult querySubmitCount()
    {
        List<DailyTaskSubmitCountBo> list = yaDailyTasksService.querySubmitCount();
        return AjaxResult.success(list);
    }



    /**
     * 查询每日任务列表
     */
    @RequiresPermissions("teaching:daily_tasks:list")
    @GetMapping("/list")
    public TableDataInfo list(YaDailyTasks yaDailyTasks)
    {
        startPage();
        List<YaDailyTasks> list = yaDailyTasksService.selectYaDailyTasksList(yaDailyTasks);
        return getDataTable(list);
    }

    /**
     * 导出每日任务列表
     */
    @RequiresPermissions("teaching:daily_tasks:export")
    @Log(title = "每日任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YaDailyTasks yaDailyTasks)
    {
        List<YaDailyTasks> list = yaDailyTasksService.selectYaDailyTasksList(yaDailyTasks);
        ExcelUtil<YaDailyTasks> util = new ExcelUtil<YaDailyTasks>(YaDailyTasks.class);
        util.exportExcel(response, list, "每日任务数据");
    }

    /**
     * 获取每日任务详细信息
     */
    @RequiresPermissions("teaching:daily_tasks:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yaDailyTasksService.selectYaDailyTasksById(id));
    }

/*    *//**
     * 新增每日任务
     *//*
    @RequiresPermissions("teaching:daily_tasks:add")
    @Log(title = "每日任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult autoCreateDailyTasks()
    {
        return toAjax(yaDailyTasksService.autoCreateDailyTasks());
    }*/

    @PostMapping
    @InnerAuth
    @Log(title = "创建每日任务", businessType = BusinessType.INSERT)
    public R<Void> createDailyTask(){
        yaDailyTasksService.autoCreateDailyTasks();
        return R.ok();
    }

    @PutMapping("/check")
    @InnerAuth
    @Log(title = "检查每日任务", businessType = BusinessType.INSERT)
    public R<Void> checkDailyTask() {
        yaDailyTasksService.checkDailyTask();
        return R.ok();
    }


    /**
     * 修改每日任务
     */
    @RequiresPermissions("teaching:daily_tasks:edit")
    @Log(title = "每日任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YaDailyTasks yaDailyTasks)
    {
        return toAjax(yaDailyTasksService.updateYaDailyTasks(yaDailyTasks));
    }

    /**
     * 删除每日任务
     */
    @RequiresPermissions("teaching:daily_tasks:remove")
    @Log(title = "每日任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yaDailyTasksService.deleteYaDailyTasksByIds(ids));
    }


    @RequiresPermissions("teaching:daily_tasks:checkAnswer")
    @GetMapping(value = "/stream/check_answer")
    public Flux<String> checkAnswer(
            @RequestParam("question") String question,
            @RequestParam("answer") String answer)
    {
        return aiService.checkAnswer(question,answer);
    }

}
